/*
ID: guoxu1
PROG: concom
LANG: C++
*/
#include <fstream>
#include <string.h>
using namespace std;

ifstream fin( "concom.in" );
ofstream fout( "concom.out" );

#define cin fin
#define cout fout

const int MAX_COMPANIES_COUNT = 100;
const int COMPANIES_ARRAY_COUNT = MAX_COMPANIES_COUNT + 1;

class Array2
{
public:
  typedef int _TABLE[COMPANIES_ARRAY_COUNT][COMPANIES_ARRAY_COUNT];
  int (*pdata)[COMPANIES_ARRAY_COUNT];
  
  Array2()
  {
    pdata = new _TABLE;
    memset( pdata, 0, sizeof( _TABLE ) );
  } 
  
  ~Array2()
  {
    delete[] pdata;
  }
  
  int* operator[] (int i) const
  {
    return pdata[i];
  }
};

class Program
{
private:
  Array2 controls;
  Array2 owns;
  
  void add_control( int a, int b )
  {
    if( controls[a][b] == 1 ) return;
    controls[a][b] = 1;
    
    for( int c = 1; c <= MAX_COMPANIES_COUNT; c++ )
    {
      owns[a][c] += owns[b][c];
      
      if( owns[a][c] >= 50 )
      {
        add_control( a, c );
      }
    }
  }
  
  void inc_own( int a, int b, int v )
  {
    for( int c = 1; c <= MAX_COMPANIES_COUNT; c++ )
    {
      if( controls[c][a] != 1 ) continue;
      owns[c][b] += v;
      if( owns[c][b] >= 50 )
      {
        add_control( c, b );
      }
    }
  }
  
public:
  int main()
  {
    for( int i = 1; i <= MAX_COMPANIES_COUNT; i++ )
    {
      controls[i][i] = 1;
    }
      
    int n = 0;
    cin >> n;

    for( int i = 0; i < n; i++ )
    {
      int i, j, p;
      cin >> i >> j >> p;
      
      inc_own( i, j, p );
    }
    
    for( int i = 1; i <= MAX_COMPANIES_COUNT; i++ )
    for( int j = 1; j <= MAX_COMPANIES_COUNT; j++ )
    {
      if( controls[i][j] == 1 && i != j )
      {
        cout << i << " " << j << endl;
      }
    }
    return 0;
  }
};


int main()
{
  return Program().main();
}
